package com.demo.spring.autowird;

import java.lang.reflect.Field;
import java.util.Optional;

import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.context.support.GenericApplicationContext;

public class Autowired底层演示5_Optional {
	
	public static void main(String[] args) throws Exception {
		GenericApplicationContext context = new GenericApplicationContext();
		context.registerBeanDefinition("bean2", new AnnotatedGenericBeanDefinition(Bean2.class));
		context.registerBeanDefinition("bean5", new AnnotatedGenericBeanDefinition(Bean5.class));
		
		Field bean2 = Bean5.class.getDeclaredField("bean2");
		DependencyDescriptor dd1 = new DependencyDescriptor(bean2, false);
		Object o = context.getDefaultListableBeanFactory().doResolveDependency(dd1, "bean5", null, null);
		System.out.println(o);
		
		Field bean3 = Bean5.class.getDeclaredField("bean3");
		DependencyDescriptor dd2 = new DependencyDescriptor(bean3, false);
		if(dd2.getDependencyType() == Optional.class) {
			// 取里面一层
			dd2.increaseNestingLevel();
			o = context.getDefaultListableBeanFactory().doResolveDependency(dd2, "bean5", null, null);
			System.out.println(o);
		}
	}

}
